<?php 
/**
* 广告类
* tengjianxing
*/
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_ad.php');

class ad
{
	var $ad_c;
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct()
	{
		$this->ad_c = new adControl();
	}
	
	/**
	* @函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{

		$url_info = $ad_info = array();
		$w_type = empty($_REQUEST['w_type'])? 0 : (INT)$_REQUEST['w_type'];//网站类型
		//分页处理
		$page = empty($_REQUEST['page']) ? 1 : (INT) $_REQUEST['page'];
		
		if($_POST['search'] || $_REQUEST['url_page'] == 1 )//搜索,分页
		{
			$ad_location	= empty($_REQUEST['ad_location']) ? '0|0|0' : trim($_REQUEST['ad_location']);
			
			$ad_size	= empty($_REQUEST['ad_size']) ? '' : trim($_REQUEST['ad_size']);
			
			$ad_name = empty($_REQUEST['ad_name']) ? '' : trim($_REQUEST['ad_name']);
			
			$ad_name = $ad_name == '广告名称' ? '' : $ad_name;
			
			$url_info = $this->ad_c->get_url_info($page,'list_search',20,$w_type,$ad_location,$ad_size,$ad_name);
			$ad_info = $this->ad_c->get_ad_info('list_search',$url_info['page_info']['start'],20,$w_type,$ad_location,$ad_size,$ad_name);
			$GLOBALS['smarty']->assign('ad_location',   $ad_location);
			$GLOBALS['smarty']->assign('ad_size',   $ad_size);
			$GLOBALS['smarty']->assign('ad_name',  $ad_name);
		}

		else
		{
			$url_info = $this->ad_c->get_url_info($page, 'list_index', 20, $w_type);
			$ad_info = $this->ad_c->get_ad_info('list_index', $url_info['page_info']['start'], 20, $w_type);
		}
		
		$delivery_info = $this->ad_c->get_delivery_size_info($w_type, 'ad_list');//获得某个网站的所有广告投放位置

		$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
		$GLOBALS['smarty']->assign('w_type',   $w_type);
		$GLOBALS['smarty']->assign('delivery_info',   $delivery_info['date']);
		$GLOBALS['smarty']->assign('size_info',   $delivery_info['size']);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('ad_info',$ad_info['date'] );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('ad_index.htm'); 

	}
	
	/**
	* @函数add_edit//增加，修改
	* @param  NULL
	* @return  string
	*/
	public function add_edit()
	{
		$w_type = empty($_REQUEST['w_type'])?0:(int)$_REQUEST['w_type'];//网站类型
		$array = array
		(
		'ad_name' =>  trim($_POST['ad_name']),
		'ad_remark' => trim($_POST['ad_remark']),
		'ad_size' => trim($_POST['ad_size']),
		'ad_photo' => trim($_POST['ad_photo']),
		'ad_alt' => trim($_POST['ad_alt']),
		'ad_url' => trim($_POST['ad_url']),
		'ad_content' => trim($_POST['ad_content'])
		
		);
		
		if($_REQUEST['act'] == 'add')
		{
			if($_POST['sub_from'])
			{
				$new_group_position_array = explode('$$',$_POST['new_group_position']);//得到新的投放位置
				$new_group_position_array = array_filter($new_group_position_array);
				
				$rows  = $this->ad_c->operate_ad('add', (INT) $_POST['ad_id'], $array, $w_type);
				if($rows['affect_nums'] > 0)
				{
					if(!empty($new_group_position_array))
					{
						$this->ad_c->operate_ad_delivery('new_add', (INT) $rows['last_id'], $new_group_position_array, $w_type);
					}
					echo  '<script type="text/javascript"> alert("增加成功！");
					window.location.href="/wondershare/ad.php?action=add_edit&act=edit&w_type='.$w_type.'&ad_id='.(INT) $rows['last_id'].'"; </script>';
					exit;
				}
			}
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('title',     '添加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->display('ad_info.htm');
			
		}elseif($_REQUEST['act'] == 'edit')
		{
			$ad_info = array();
			if($_POST['sub_from'] && !empty($_POST['ad_id']))
			{
				//删除队列
				queueDelete(0, $_POST['ad_id']);
				
					$base_arr_new = array();
					$new_group_position_array = explode('$$', $_POST['new_group_position']);//得到新的投放位置
					$old_group_position_array = explode('$$', $_POST['old_group_position']);//得到开始的投放位置
					
					$add_group_position_array  = array_filter(array_diff($new_group_position_array, $old_group_position_array)); // 得到要增加的投放位置
					$delete_group_position_array  = array_filter(array_diff($old_group_position_array, $new_group_position_array)); // 得到要删除的投放位置
					
					$new_group_position_array = array_filter($new_group_position_array);
					$rows  = $this->ad_c->operate_ad('edit', (INT) $_POST['ad_id'], $array,$w_type);
			
					if($rows['affect_nums'] > 0)
					{
						if(empty($old_group_position_array))
						{
							$this->ad_c->operate_ad_delivery('new_add', (INT) $_POST['ad_id'], $new_group_position_array, $w_type);
						
						}else
						{
							if(!empty($add_group_position_array))
							{
							
								$this->ad_c->operate_ad_delivery('new_add', (INT) $_POST['ad_id'], $add_group_position_array, $w_type);
							}
							
							if(!empty($delete_group_position_array))
							{
								$this->ad_c->operate_ad_delivery('del', (INT) $_POST['ad_id'], $delete_group_position_array, $w_type);
							}
						}
						echo  '<script type="text/javascript"> alert("编辑成功！");
						window.location.href="/wondershare/ad.php?action=add_edit&act=edit&w_type='.$w_type.'&ad_id='.(int)$_POST['ad_id'].'"; </script>';
						exit;
					}
			}
			
			if(!empty($w_type) && !empty($_GET['ad_id'])){
				//加入队列
				queueInfo($_GET['ad_id']);
				$ad_info = $this->ad_c->get_ad_id_info((INT) $_GET['ad_id'], $w_type);
			}
	
			$GLOBALS['smarty']->assign('new_group_position',   $ad_info['group_position']);//获得某个广告的投放位置
			$GLOBALS['smarty']->assign('old_group_position',   $ad_info['group_position']);//获得某个广告的投放位置
		
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('title',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('ad_info', $ad_info);
			$GLOBALS['smarty']->display('ad_info.htm'); 	
			
			
		}elseif($_REQUEST['act'] == 'is_name_exist')//ajax判断是否有重复名称出现
		{
			$where_str = !empty($_GET['ad_id']) ? ' AND  ad_id != '.$_GET['ad_id']:'';
			$sql = 'SELECT count(*) AS nums  FROM ad WHERE ad_name = "'.trim($_GET['ad_name']).'"'.$where_str.' AND is_delete = 0 AND ad_web = '.$w_type;
			$nums = $GLOBALS['db']->getOne($sql);
			
			echo  $nums;
		}
	
	}
	
	/**
	* @函数del
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->ad_c->delete_ad($checkList);
		}
	}
	
	/**
	* 广告位列表
	* @param  NULL
	* @return  string
	*/
	public function ad_position_list()
	{
		
		$new_group_position_array = array();
		
		$w_type = empty($_REQUEST['w_type'])? 0 : (int)$_REQUEST['w_type'];//网站类型
		
		$new_group_position = empty($_REQUEST['new_group_position'])?'':$_REQUEST['new_group_position'];//得到新组合投放位置
		
		$old_group_position = empty($_REQUEST['old_group_position'])?'':$_REQUEST['old_group_position'];//得到编辑组合投放位置
		
		$ad_id = empty($_REQUEST['ad_id']) ? '' : $_REQUEST['ad_id'];//广告ID
		
		$new_group_position_array = array_filter(explode('$$', $new_group_position));//分割新组合投放位置并且以$$为分割线的数组
		
		//判断选择的 广告是否存在
		if($_REQUEST['type'] == 'is_exist_position')
		{
			$ad_position_name_str = '';//得到已经投放位置的广告名称 以及产品名称或者文章类别名称组合
			$exist_ad_position = ''; //设置已经存在的 投放位置的广告名称 以及产品名称或者文章类别名称组合
			$old_group_position_array = array_filter(explode('$$', $old_group_position));//分割旧组合投放位置并且以$$为分割线的数组
			$new_group_position = array();//初始化除去产品名称以及文章分类的投放位置数组
			$exist_ad_position = '';////初始化已经存在的投放位置说明用于提示
			$diff_group_position = '';////初始化新与旧的差异数组
			$new_group = array();////初始化新数组
			$new_group_position_str = '' ; //新的 投放字符串
			foreach ($new_group_position_array  as $key => $value)//差集
			{
				$row = explode("&&%",$value);//用&&%字符分割广告位名称（或分类名称）和广告名称
				$position_row = explode("|", $row[2]);//用|字符分割广告投放位置
				if($position_row[2] == 1)
				{
				
					$ad_position_name_str .= chr(13).'    -- 文章'.$row[0].'  '.$row[1];//chr(13)  等于 '/n'
				}elseif($position_row[2] == 5)
				{
					$ad_position_name_str .= chr(13).'    -- 产品'.$row[0].'  '.$row[1];//chr(13)  等于 '/n'
				}
				else
				{
					$ad_position_name_str .= chr(13).$row[0].' '.$row[1];
				}
				
				$new_group_position[$key] = $row[2];
				
				$new_group_position_str .= empty($new_group_position_str) ?  $row[2] : '$$'.$row[2];
				
				$new_group[$row[2]] = $row;
			}
			
			$diff_group_position = array_diff($new_group_position,$old_group_position_array);
			
			if(!empty($diff_group_position))
			{
				$exist_ad_position = $this->ad_c->get_exist_ad_position($diff_group_position, $new_group, $w_type);//得到已经存在的位置
			
			}
			
			unset($row, $position_row, $diff_group_position, $old_group_position_array,$new_group_position);//销毁数组
			
			print_r(json_encode(array('ad_position_name_str' => $ad_position_name_str,
			'new_group_position' => $new_group_position_str,'tips' => empty($exist_ad_position) ? '' : $exist_ad_position)));
		}else 
		{
			
			$delivery_info = $this->ad_c->get_delivery_size_info($w_type,'ad_edit',$new_group_position_array);//获得某个网站的所有广告投放位置
			$GLOBALS['smarty']->assign('select_delivery_info',   $delivery_info['date']);//获取所有广告投放位置用于下拉框显示
			if($_POST['search'])//以下判断处理弹窗口搜索
			{
				$new_delivery_info = array();
				
				$ad_location = empty($_POST['ad_location'])? '' :trim($_POST['ad_location']);
				
				if($ad_location != '0|0|0')//不等于搜索全部
				{
					$ad_location_array = explode('|', $ad_location);
				
					$cat_info_array = array();
		
					if($ad_location_array[1] == 0)//表示某个投放位置
					{
						foreach ($delivery_info['date'] AS $key => $values)
						{
					
							if($values['position_id'] == $ad_location_array[0])
							{
								
								$new_delivery_info['date'][0] = $values;
			
								break;
							}
						}
					}elseif($ad_location_array[1] != 0)//表示选择了文章或者产品下面的某个文章分类（主要用于产品和文章）
					{
						foreach ($delivery_info['date'] AS $key => $values)
						{
							if($values['position_id'] == $ad_location_array[0])
							{
								foreach ($values['cat_info'] AS $cat_key => $cat_value)
								{
									if($cat_value['cat_id'] == $ad_location_array[1])
									{
										$cat_info_array[0] = $cat_value;
										break;
									}
								}
								unset($values['cat_info']);
								$new_delivery_info['date'][0] = $values;
								
								$new_delivery_info['date'][0]['cat_info'] = $cat_info_array;
								
								
								break;
							}
						}
					}
					$delivery_info = $new_delivery_info;
					unset($new_delivery_info, $cat_info_array);
					
				}

				$GLOBALS['smarty']->assign('ad_location',   $ad_location);
				
			}
			
			$GLOBALS['smarty']->assign('new_group_position',   $new_group_position);
	
			$GLOBALS['smarty']->assign('old_group_position',   $old_group_position);
			
			$GLOBALS['smarty']->assign('ad_id',   $ad_id);
			$GLOBALS['smarty']->assign('w_type',   $w_type);

			$GLOBALS['smarty']->assign('delivery_info_list',   $delivery_info['date']);
			
			$GLOBALS['smarty']->display('ad_delivery_list.htm');
		}
		
	}

}
$action    = $_REQUEST['action']; 
$ad   = new ad;
if(method_exists($ad, $action)) 
{
	$ad->$action();
}else{
	$ad->index();
}

?>
